package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
       try{
           //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
           User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println(user);
           if(user != null){
               String username = user.getUsername();
               return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
           }
       }catch (Exception e){

       }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);

    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.pageQuery(queryPageBean);
    }

    //添加新用户
    @RequestMapping("/add")
    public Result addUser(@RequestBody User user){
        try{
            userService.addUser(user);
//            System.out.println(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);//新增失败
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);//新增成功

    }
    //根据id查询用户
    @RequestMapping("/findById")
    public Result findById(int id){
     try{
         User user = userService.findById(id);
         return  new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
     }catch (Exception e){
         return  new Result(false, MessageConstant.QUERY_USER_FAIL);
     }
    }
    //编辑用户信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user){
     try{
         userService.edit(user);
         return  new Result(true, MessageConstant.EDIT_USER_SUCCESS,user);
     }catch (Exception e){
         return  new Result(false, MessageConstant.EDIT_USER_FAIL);
     }
    }
    //删除
    @RequestMapping("/delete")
    public Result delete(int id){
     try{
         userService.delete(id);
         return  new Result(true, MessageConstant.DELETE_USER_SUCCESS);
     }catch (Exception e){
         return  new Result(false, MessageConstant.DELETE_USER_FAIL);
     }
    }

    //根据用户查询roleid
    @RequestMapping("/findroleIdsByuserId")
    public Result findroleIdsByuserId(int id){
     try{
      List<Integer> checkroleIds =  userService.findroleIdsByuserId(id);
         return  new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,checkroleIds);
     }catch (Exception e){
         return  new Result(false, MessageConstant.QUERY_ROLE_FAIL);
     }
    }

    //给用户赋予角色
    @RequestMapping("/addRole")
    public Result addRole(Integer [] checkroleIds, Integer userid){
     try{
        userService.addRole(checkroleIds,userid);
//         System.out.println(checkroleIds);
//         System.out.println(userid);
         return  new Result(true, MessageConstant.GIVE_ROLE_SUCCESS);
     }catch (Exception e){
         return  new Result(false, MessageConstant.GIVE_ROLE_FAIL);
     }
    }





}
